package com.ktjiaoyu.controller;

import com.alibaba.fastjson2.JSON;
import com.ktjiaoyu.entity.Supplier;
import com.ktjiaoyu.entity.SysRole;
import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.service.SysRoleService;
import com.ktjiaoyu.service.SysUserService;
import com.ktjiaoyu.utils.Constants;
import com.ktjiaoyu.utils.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author 周颖
 * @Date 2024/12/24 14:58
 **/
@Controller
@RequestMapping("/main/role")
public class SysRoleController extends BaseController{
    private static final Logger LOGGER = LoggerFactory.getLogger(SysRoleController.class);

    @Autowired
    private SysRoleService sysRoleService;

    @RequestMapping("/list")
    public String list(Model model){
        try {
            //查询所有的角色信息
            List<SysRole> roleList = sysRoleService.findAll();
            model.addAttribute("list", roleList);
        }catch (Exception e){
            LOGGER.error("SysRoleController中的list方法执行发生错误。",e);
            throw new RuntimeException(e);
        }
        return "role/list";
    }

    @GetMapping("/save")
    public String toSave(Model model){
        try{
            model.addAttribute("SysRole", new SysRole());
        }catch (Exception e){
            LOGGER.error("SysRoleController中的toSave方法执行发生错误。",e);
            throw new RuntimeException(e);
        }
        return "role/save";
    }

    @PostMapping("/save")
    public String doSave(SysRole sysRole){
        try{
            SysUser loginUser = session(Constants.SESSION_USER);
            if(sysRole.getId() == null){
                sysRole.setCreatedUserId(loginUser.getId());
                sysRole.setCreatedTime(new Date());
            }else{
                sysRole.setUpdatedUserId(loginUser.getId());
                sysRole.setUpdatedTime(new Date());
            }
            boolean ok = sysRoleService.saveOrUpdate(sysRole);
            if(ok){
                return redirect("/main/role/list");
            }
        }catch (Exception e){
            LOGGER.error("SysRoleController中的doSave方法执行发生错误。",e);
            throw new RuntimeException(e);
        }
        return "role/save";
    }

    @GetMapping("/view/{id}")
    public String view(@PathVariable("id") Long id, Model model){
        SysRole sysRole = null;
        try{
            sysRole = sysRoleService.findById(id);
            model.addAttribute("sysRole",sysRole);
        }catch (Exception e){
            LOGGER.error("SysRoleController中的view方法执行发生错误。",e);
            throw new RuntimeException(e);
        }
        return "role/view";
    }

    @GetMapping("/edit")
    public String toEdit(@RequestParam("id") Long id,Model model){
        SysRole sysRole = null;
        try{
            sysRole = sysRoleService.findById(id);
            model.addAttribute("sysRole",sysRole);
        }catch (Exception e){
            LOGGER.error("SysRoleController中的toEdit方法执行发生错误。",e);
            throw new RuntimeException(e);
        }
        return "role/save";
    }

    @ResponseBody
    @GetMapping("/codeExist")
    public String codeExist(@RequestParam String code){
        LOGGER.debug("检查用户名Code={}是否存在",code);
        HashMap<String,Object> resultMap = new HashMap<String,Object>();
        if(StringUtils.isEmpty(code)){
            resultMap.put("exist",0);
        }else{
            try{
                SysRole sysRole = sysRoleService.findByCode(code);
                if(null != sysRole){
                    resultMap.put("exist",1);
                }else {
                    resultMap.put("exist",0);
                }
            }catch (Exception e){
                LOGGER.error("SysRoleController中的accountExist方法执行发生错误",e);
                resultMap.put("error","出现异常，请联系管理员处理！");
            }
        }
        return JSON.toJSONString(resultMap);
    }

    @GetMapping(value = "/{id}/view")
    @ResponseBody
    public Map ajaxView(@PathVariable("id") Long id){
        HashMap<String,Object> resultMap = new HashMap<String ,Object>();
        SysRole info = null;
        try{
            info = sysRoleService.findById(id);
            if(info != null){
                resultMap.put("code",200);
                resultMap.put("data",info);
            }else{
                resultMap.put("code",500);
            }
        }catch (Exception e){
            LOGGER.error("SysRoleController中的ajaxView方法执行发生错误。",e);
            resultMap.put("code",500);
        }
        return resultMap;
    }

    @DeleteMapping("/del/{id}")
    @ResponseBody
    public Map del(@PathVariable("id") Long id){
        HashMap<String,Object> resultMap = new HashMap<String ,Object>();
        try{
            boolean ok = sysRoleService.deleteById(id);
            if(ok){
                sysRoleService.deleteById(id);
                resultMap.put("code",200);
            }else{
                resultMap.put("code",500);
            }
        }catch (Exception e){
            LOGGER.error("SysRoleController中的del方法执行发生错误。",e);
            throw new RuntimeException(e);
        }
        return resultMap;
    }
}
